package com.csyf.manager.dao;

import com.csyf.manager.entity.SysAgentBalance;
import com.csyf.manager.entity.SysOrderProfit;
import org.apache.commons.lang.StringUtils;

/**
 * @Author: Enzo
 * @Date: 2018/5/31 10:15
 */
public class SysBalanceSqlProvider {

    public String sumTradeFee(SysAgentBalance vo) {

        StringBuffer sql = new StringBuffer("SELECT IFNULL(SUM(IFNULL(totalmoney,0)),0)totalmoney,IFNULL(SUM(IFNULL(totalprofit,0)),0)totalprofit,IFNULL(SUM(IFNULL(totalcashout,0)),0)totalcashout,IFNULL(SUM(IFNULL(unclearmoney,0)),0)unclearmoney FROM sys_agent_balance WHERE 1=1");
        if(null != vo.getAgentId()){
            sql.append(" AND agent_id = #{agentId}");
        }
        if(StringUtils.isNotBlank(vo.getMchId())){
            sql.append(" AND mch_id = #{mchId}");
        }
        if(StringUtils.isNotBlank(vo.getCurdate())){
            sql.append(" AND curdate = #{curdate}");
        }
        if(StringUtils.isNotBlank(vo.getService())){
            sql.append(" AND service = #{service}");
        }

        return sql.toString();
    }



    public String sumprofit(SysOrderProfit vo) {

        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("select " +
                "ifnull(sum(trade_count),0)tradeCount," +
                "ifnull(sum(trade_totalmoney),0)tradeTotalmoney," +
                "ifnull(sum(cost_Profit),0)costProfit," +
                "ifnull(sum(mch_profit),0)mchProfit " +
                "from sys_order_profit");
        stringBuffer.append(" where ");
        stringBuffer.append("1=1 ");
        if(StringUtils.isNotBlank(vo.getMchId())){
            stringBuffer.append(" and mch_id = #{mchId}");
        }
        if(StringUtils.isNotBlank(vo.getSrcMchId())){
            stringBuffer.append(" and src_mch_id = #{srcMchId}");
        }

        if(StringUtils.isNotBlank(vo.getCtime())){
            stringBuffer.append(" and ctime = #{ctime}");
        }

        if(StringUtils.isNotBlank(vo.getServiceType())){
            stringBuffer.append(" and service_type = #{serviceType}");
        }

        stringBuffer.append("order by CTIME DESC,MCH_ID ASC");

        return stringBuffer.toString();
    }
}
